'''
case Type :数据库系统
case Name ：备机数据库被kill掉主进程后，备机重建成功
Create At: 2020-6-28
owner : @peilinqian
description:1、kill备机2、重建备机成功
history：
created： 2020/06/28  created by @peilinqian
'''
import os
import time
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case017.py start------------')
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.rootNode = Node(node='PrimaryRoot')
        self.dbUserNode = Node(node='PrimaryDbUser')
        self.standbyrootNode = Node(node='Standby1Root')
        self.standbydbUserNode = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()

    def test_main(self):
        # kill掉备机进程
        msg = self.com.kill_pid(self.standbyrootNode, 9)
        self.assertEqual(msg, '')

        time.sleep(3)

        db_status = self.primary_sh.getDbClusterStatus('status')
        self.assertFalse(db_status)

        # 重建备机
        msg = self.standby1_sh.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
        self.assertTrue(msg)

    def tearDown(self):
        db_status = self.primary_sh.getDbClusterStatus('status')
        if not db_status:
            msg = self.standby1_sh.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG, '-b full')
            self.assertTrue(msg)
